package com.koron.tool.dialog;

import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

import com.koron.tool.frame.UserDefineFrame;
import com.koron.ui.MutableGridLayout;

@SuppressWarnings("serial")
public class FieldFlagDialog extends JDialog {
	private UserDefineFrame frame;

	private int realValue = 0;
	private IntegerCheckbox jcbMust = new IntegerCheckbox("必填",1);
	private IntegerCheckbox jcbUnique = new IntegerCheckbox("唯一",2);
	private IntegerCheckbox jcbIdentity = new IntegerCheckbox("递增",4);
	private IntegerCheckbox jcbSearch = new IntegerCheckbox("修改框(不选为条件框)",8);
	
	public FieldFlagDialog(final UserDefineFrame frame,int value) {
		super(frame.frame,true);
		this.frame = frame;
		init();
		setValue(value);
	}
	
	private void init() {
		MutableGridLayout layout = new MutableGridLayout(2, 3, 5, 5);
		layout.setHeight(2, 30);
		setLayout(layout);
		add(jcbMust);
		add(jcbUnique);
		add(jcbIdentity);
		add(jcbSearch);
		add(new JButton(new OkAction(this)));
		add(new JButton(new CancelAction(this)));
		setSize(300, 300);
		Point p = frame.getLocationOnScreen();
		p.x = p.x + (frame.getWidth() - getWidth()) / 2;
		p.y = p.y + (frame.getHeight() - getHeight()) / 2;
		setLocation(p);
	}

	/**
	 * 
	 */
	private void setValue(int value)
	{
		this.realValue = value;
		if((value & jcbMust.getValue()) != 0)
			jcbMust.setSelected(true);
		if((value & jcbUnique.getValue()) != 0)
			jcbUnique.setSelected(true);
		if((value & jcbIdentity.getValue()) != 0)
			jcbIdentity.setSelected(true);
		if((value & jcbSearch.getValue()) != 0)
			jcbSearch.setSelected(true);
	}
	
	public int getValue()
	{
		return realValue;
	}
	
	class CheckListener implements ActionListener {
		@Override
		public void actionPerformed(ActionEvent e) {
			try {
				IntegerCheckbox tmp = (IntegerCheckbox)e.getSource();
				if(tmp.isSelected())
					realValue = realValue | tmp.getValue();
				else {
					realValue = realValue & ~tmp.getValue();
				}
			} catch (NumberFormatException e1) {
				e1.printStackTrace();
			}
		}
	}
	
	class OkAction extends AbstractAction
	{
		FieldFlagDialog dialog;
		public OkAction(FieldFlagDialog dialog)
		{
			this.dialog = dialog;
			putValue(NAME, "确定");
		}
		
		@Override
		public void actionPerformed(ActionEvent e) {
			dialog.setVisible(false);
		}
	}
	
	class CancelAction extends AbstractAction
	{
		FieldFlagDialog dialog;
		public CancelAction(FieldFlagDialog dialog)
		{
			this.dialog = dialog;
			putValue(NAME, "取消");
		}
		@Override
		public void actionPerformed(ActionEvent e) {
			dialog.setVisible(false);
		}
	}
	class IntegerCheckbox extends JCheckBox
	{
		int value;
		public IntegerCheckbox(String caption,int value)
		{
			super(caption);
			this.value=value;
			this.addActionListener(new CheckListener());
		}
		/**
		 * @return 获取#{bare_field_comment}
		 */
		public int getValue() {
			return value;
		}
		/**
		 * @param 设置#{bare_field_comment}
		 */
		public void setValue(int value) {
			this.value = value;
		}
		
	}
}